﻿using NeoAPI;

namespace Baumer_neoAPI.Utilities
{
    /// <summary>
    /// 液态镜头控制帮助类
    /// </summary>
    public class LiquidLensHelper : IDisposable
    {
        private readonly Cam _camera;
        private bool _isInitialized = false;

        public LiquidLensHelper()
        {
            _camera = new Cam();
        }

        /// <summary>
        /// 连接相机并初始化液态镜头
        /// </summary>
        public void ConnectAndInitialize()
        {
            try
            {
                Console.WriteLine("正在搜索相机...");
                _camera.Connect();
                Console.WriteLine($"已连接相机: {_camera.f.DeviceModelName}");

                InitializeLiquidLens();
                _isInitialized = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine($"初始化失败: {ex.Message}");
                throw;
            }
        }

        /// <summary>
        /// 设置液态镜头屈光度
        /// </summary>
        /// <param name="diopter">屈光度值(单位:dpt)</param>
        public void SetDiopter(double diopter)
        {
            if (!_isInitialized)
                throw new InvalidOperationException("液态镜头未初始化");

            if (!_camera.HasFeature("FocalPower"))
                throw new NotSupportedException("该相机不支持液态镜头控制");

            Console.WriteLine($"设置屈光度为: {diopter} dpt");
            _camera.f.FocalPower.Value = diopter;

            // 获取当前实际屈光度（验证设置）
            double currentDpt = (double)_camera.f.FocalPower.Value;
            Console.WriteLine($"实际屈光度: {currentDpt} dpt");
        }

        /// <summary>
        /// 获取当前屈光度
        /// </summary>
        public double GetCurrentDiopter()
        {
            if (!_isInitialized)
                throw new InvalidOperationException("液态镜头未初始化");

            if (!_camera.HasFeature("FocalPower"))
                throw new NotSupportedException("该相机不支持液态镜头控制");

            return (double)_camera.f.FocalPower.Value;
        }

        /// <summary>
        /// 捕获当前图像
        /// </summary>
        /// <param name="filename">保存文件名</param>
        public void CaptureImage(string filename)
        {
            if (!_isInitialized)
                throw new InvalidOperationException("液态镜头未初始化");

            // 启用固定模式噪声校正（如果可用）
            if (_camera.HasFeature("FixedPatternNoiseCorrection"))
            {
                _camera.f.FixedPatternNoiseCorrection.Value = true;
            }

            var image = _camera.GetImage();
            if (image != null)
            {
                image.Save(filename);
                Console.WriteLine($"已保存图像: {filename}");
            }
            else
            {
                throw new Exception("捕获图像失败");
            }
        }

        /// <summary>
        /// 初始化液态镜头
        /// </summary>
        private void InitializeLiquidLens()
        {
            // 检查是否支持光学控制器初始化
            if (_camera.HasFeature("OpticControllerInitialize"))
            {
                Console.WriteLine("正在初始化光学控制器...");
                _camera.f.OpticControllerInitialize.Execute();
            }

            // 初始化液态镜头焦距（如果支持）
            if (_camera.HasFeature("FocalLengthInitialize"))
            {
                Console.WriteLine("正在初始化液态镜头焦距...");
                _camera.f.FocalLengthInitialize.Execute();
            }

            // 初始化对焦系统（如果支持）
            if (_camera.HasFeature("FocusInitialize"))
            {
                Console.WriteLine("正在初始化对焦系统...");
                _camera.f.FocusInitialize.Execute();
            }

            Console.WriteLine("初始化完成");
        }

        public void Dispose()
        {
            _camera?.Dispose();
        }
    }
}
